/// <reference path="typings/sharepoint/sharepoint.d.ts" />
/// <reference path="typings/jquery/jquery.d.ts" />
'use strict';
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
var ANNOUNCEMENT_LIST_NAME = "New Announcements List";
var _operations;
function initializePage() {
    $(document).ready(function () {
        _operations = new ListOperations();
    });
}
function btnOperationOnClick() {
    switch ($('#optList').val()) {
        case "Get All Announcement Lists":
            _operations.listObjects();
            break;
        case "Add a new Announcement List":
            _operations.addObject(ANNOUNCEMENT_LIST_NAME);
            break;
        case "Update Announcement List":
            _operations.updateObject(ANNOUNCEMENT_LIST_NAME);
            break;
        case "Delete First Announcement List":
            _operations.deleteObject(ANNOUNCEMENT_LIST_NAME);
            break;
        default:
            console.log("invalid case");
    }
}
var Operations = (function () {
    function Operations(operationType) {
        this.OperationType = operationType;
    }
    return Operations;
})();
var WebOperations = (function (_super) {
    __extends(WebOperations, _super);
    function WebOperations() {
        _super.call(this, "Web Operations");
    }
    WebOperations.prototype.listObjects = function () { };
    WebOperations.prototype.addObject = function (title) { };
    WebOperations.prototype.updateObject = function (title) { };
    WebOperations.prototype.deleteObject = function (title) { };
    WebOperations.prototype.logError = function (sender, args) { };
    return WebOperations;
})(Operations);
var ListOperations = (function (_super) {
    __extends(ListOperations, _super);
    function ListOperations() {
        _super.call(this, "List Operations");
        var hostUrl = decodeURIComponent(this.getQueryStringParameter("SPHostUrl"));
        this._currentContext = SP.ClientContext.get_current();
        this._hostContext = new SP.AppContextSite(this._currentContext, hostUrl);
        this._webSite = this._hostContext.get_web();
    }
    ListOperations.prototype.listObjects = function () {
        var _this = this;
        $('#message').text("fetching announcement lists...");
        this._collList = this._webSite.get_lists();
        this._currentContext.load(this._collList); //, 'Include(Title, Id)');
        this._currentContext.executeQueryAsync(function () { return _this._onlistObjectsSucceeded(); }, function (s, e) { return _this.logError(_this, e); });
    };
    ListOperations.prototype._onlistObjectsSucceeded = function () {
        var listInfo = '';
        var listEnumerator = this._collList.getEnumerator();
        while (listEnumerator.moveNext()) {
            var list = listEnumerator.get_current();
            var listTemplateType = list.get_baseTemplate();
            if (listTemplateType === SP.ListTemplateType.announcements) {
                listInfo += 'Title: ' + list.get_title() + " " + "<br/>";
                listInfo += 'Description: ' + list.get_description() + " " + "<br/><br/>";
            }
        }
        $('#message').html(listInfo);
    };
    ListOperations.prototype.logError = function (sender, args) {
        $('#message').text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    };
    ListOperations.prototype.addObject = function (listName) {
        var _this = this;
        $('#message').text("creating announcement list...");
        var listCreationInfo = new SP.ListCreationInformation();
        listCreationInfo.set_title(listName);
        listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
        this._list = this._webSite.get_lists().add(listCreationInfo);
        this._currentContext.load(this._list);
        this._currentContext.executeQueryAsync(function () { return _this._onAddObjectSucceeded(); }, function (s, e) { return _this.logError(_this, e); });
    };
    ListOperations.prototype._onAddObjectSucceeded = function () {
        var result = ANNOUNCEMENT_LIST_NAME + ' created.';
        $('#message').text(result);
    };
    ListOperations.prototype.updateObject = function (listName) {
        var _this = this;
        $('#message').text("updating announcement list...");
        this._list = this._webSite.get_lists().getByTitle(listName);
        this._list.set_description(listName + " ---updated");
        this._list.update();
        this._currentContext.load(this._list);
        this._currentContext.executeQueryAsync(function () { return _this._onUpdateObjectSucceeded(); }, function (s, e) { return _this.logError(_this, e); });
    };
    ListOperations.prototype._onUpdateObjectSucceeded = function () {
        var result = ANNOUNCEMENT_LIST_NAME + ' updated.';
        $('#message').text(result);
    };
    ListOperations.prototype.deleteObject = function (listName) {
        var _this = this;
        $('#message').text("deleting announcement list...");
        this._list = this._webSite.get_lists().getByTitle(listName);
        this._list.deleteObject();
        this._currentContext.executeQueryAsync(function () { return _this._onDeleteObjectSucceeded(); }, function (s, e) { return _this.logError(_this, e); });
    };
    ListOperations.prototype._onDeleteObjectSucceeded = function () {
        var result = ANNOUNCEMENT_LIST_NAME + ' deleted.';
        $('#message').text(result);
    };
    ListOperations.prototype.getQueryStringParameter = function (paramToRetrieve) {
        var params = document.URL.split("?")[1].split("&");
        var strParams = "";
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    };
    return ListOperations;
})(Operations);
//# sourceMappingURL=App.js.map